import csv
import numpy as np
import main_activity.writefile


def load_file(file_name):
    value = []

    with open(file_name, 'r', encoding='utf-8') as f:
        f_csv = csv.reader(f)
        for row in f_csv:
            value.append(row)
        return value


def main(file_path):
    d = 0
    value = load_file(file_path)
    value.sort()
    value.reverse()
    value = np.array(value)
    for i in range(1, len(value) - 1):
        if i >= len(value) - 1:
            break
        try:
            if value[i][4] == value[i + 1][4]:
                d = d + 1
                print(value[i][0])
                value[i][7] = value[i][7] + "," + value[i + 1][7]
                value[i][6] = str(float(value[i][6]) + float(value[i + 1][6]))
                value = np.delete(value, i + 1, axis=0)
        except:
            print(value[i])
            print(value[i+1])

    main_activity.writefile.main(file_path, value)
    return d
